package com.platform.tony.utils;


import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;

//import javax.xml.bind.DatatypeConverter;

import java.util.Date;

@Slf4j
public class JwtUtils {

    private static final String secretKey = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9"; //进行数字签名的私钥，一定要保管好

    public static void main(String[] args) throws Exception {

        //获取系统的当前时间
        long ttlMillis = System.currentTimeMillis();
        Date now = new Date(ttlMillis);

        //生成jwt令牌
        JwtBuilder jwtBuilder = Jwts.builder()
                .setId("66")//设置jwt编码
                .setSubject("程序员")//设置jwt主题
                .setIssuedAt(new Date())//设置jwt签发日期
                //.setExpiration(date)//设置jwt的过期时间
                .claim("t", "admin")
                .claim("company", "itheima")
                .signWith(SignatureAlgorithm.HS256, secretKey);
        //生成jwt
        String jwtToken = jwtBuilder.compact();
        System.out.println(jwtToken);

        // String s = acquireJWT("123456", "1", "123");
        // byte[] bytes = DatatypeConverter.parseBase64Binary(secretKey);
        //解析jwt,得到其内部的数据
        Claims claims = Jwts.parser().setSigningKey(secretKey).parseClaimsJws(jwtToken).getBody();
        System.out.println(claims);
    }

    /**
     * 生成串
     *
     * @param token
     * @param id
     * @param account
     * @return
     * @throws Exception
     */
    public static String acquireJWT(String token, String id, String account, Integer uid) {
        //生成jwt令牌
        JwtBuilder jwtBuilder = Jwts.builder()
                .setId(id)//设置jwt
                .setSubject("subject")//设置jwt主题
                .setIssuedAt(new Date())//设置jwt签发日期
                //.setExpiration(date)//设置jwt的过期时间
                .claim("t", token)
                .claim("account", account)
                .claim("uid", uid)
                .claim("company", "itheima")
                .signWith(SignatureAlgorithm.HS256, secretKey);

        return jwtBuilder.compact();
    }

    /**
     * 解析JWT字符串
     *
     * @param jwt
     * @return
     * @throws Exception
     */
    public static Claims parseJWT(String jwt) {
        return Jwts.parser()
                .setSigningKey(secretKey)
                .parseClaimsJws(jwt)
                .getBody();
    }

//    public String RefreshToken(String token) {
//        final Date createdDate = clock.now();
//        final Date expirationDate = calculateExpirationDate(createdDate);
//        final Claims claims = getAllClaimsFromToken(token);
//        claims.setIssuedAt(createdDate);
//        claims.setExpiration(expirationDate);
//        return Jwts.builder()
//                .setClaims(claims)
//                .signWith(SignatureAlgorithm.HS512, secretKey)
//                .compact();
//    }

    private final Integer expiration = 2000;

    private Date calculateExpirationDate(Date createdDate) {
        return new Date(createdDate.getTime() + expiration * 1000);
    }


}
